package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Option;
import com.example.domain.Question;
import com.example.service.OptionService;
import com.example.mapper.OptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DPH
 * @description 针对表【tbl_option】的数据库操作Service实现
 * @createDate 2022-11-25 23:18:12
 */
@Service
public class OptionServiceImpl extends ServiceImpl<OptionMapper, Option>
        implements OptionService{
    @Autowired
    private OptionMapper optionMapper;

    @Override
    public List<Option> getOption2(int parseInt) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id",parseInt);
        return optionMapper.selectList(queryWrapper);
    }



    @Override
    public List<Option> getOption(int parseInt) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",parseInt);
        return optionMapper.selectList(queryWrapper);
    }

    @Override
    public void delete(Integer questionId) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",questionId);
        optionMapper.delete(queryWrapper);
    }

    @Override
    public List<Option> getOptions(Integer questionnaireid, Integer questionid) {
        QueryWrapper<Option> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireid);
        wrapper.eq("question_id",questionid);
        return optionMapper.selectList(wrapper);
    }

    @Override
    public Option modify(Option option) {
        return optionMapper.updateById(option) == 1 ? option : null;
    }

    @Override
    public Option findById(Integer questionnaireid, Integer questionid, Integer optionid) {
        QueryWrapper<Option> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireid);
        wrapper.eq("question_id",questionid);
        wrapper.eq("option_num",optionid);
        return optionMapper.selectOne(wrapper);
    }

    @Override
    public boolean delete(Integer questionnaireid, Integer questionid, Integer optionid) {
        QueryWrapper<Option> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireid);
        wrapper.eq("question_id",questionid);
        wrapper.eq("option_num",optionid);
        return optionMapper.delete(wrapper) > 0;
    }

    @Override
    public boolean add(Option option) {
        return optionMapper.insert(option) > 0;
    }
}
